package com.diamondparking.action;

import com.diamondparking.model.Car;
import com.diamondparking.model.Claim;
import com.diamondparking.model.Color;
import com.diamondparking.model.Employee;
import com.diamondparking.model.Location;
import com.diamondparking.model.Make;
import java.util.Date;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.faces.application.FacesMessage;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityController;

@Stateful
@Name("claimManager")
public class ClaimManagerBean extends EntityController implements ClaimManager {

    private String customerName;
    private String locationParked;
    private String parkedBy;
    private String licensePlate;
    private String make;
    private Color color;
    private boolean carWash;
    private String comments;
    private String damageReport;

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public boolean isCarWash() {
        return carWash;
    }

    public void setCarWash(boolean carWash) {
        this.carWash = carWash;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getDamageReport() {
        return damageReport;
    }

    public void setDamageReport(String damageReport) {
        this.damageReport = damageReport;
    }

    public String getLicensePlate() {
        return licensePlate;
    }

    public void setLicensePlate(String licensePlate) {
        this.licensePlate = licensePlate;
    }

    public String getLocationParked() {
        return locationParked;
    }

    public void setLocationParked(String locationParked) {
        this.locationParked = locationParked;
    }

    public String getParkedBy() {
        return parkedBy;
    }

    public void setParkedBy(String parkedBy) {
        this.parkedBy = parkedBy;
    }

    @End
    public void create() {
        Claim newClaim = new Claim();
        newClaim.setEntered(new Date());
        newClaim.setDamageReport(damageReport);
        newClaim.setCustomer(customerName);
        newClaim.setCarWash(carWash);
        newClaim.setComments(comments);

        Employee employee = (Employee) createQuery("SELECT e FROM Employee e WHERE e.username = :username")
                .setParameter("username", parkedBy)
                .getSingleResult();
        if (employee != null) {
            newClaim.setParkedBy(employee);
        } else {
            getFacesMessages().add("You must enter a valid employee");
            clear();
            return;
        }

        Car car = (Car) createQuery("SELECT c FROM Car c WHERE c.licensePlate = :licensePlate")
                .setParameter("licensePlate", licensePlate)
                .getSingleResult();
        if (car != null) {
            newClaim.setCar(car);
        } else {
            Car newCar = new Car();
            newCar.setLicensePlate(licensePlate);
            newCar.setColor(color);

            Make makeOb = (Make) createQuery("SELECT m FROM Make m WHERE m.name = :name")
                    .setParameter("name", make)
                    .getSingleResult();
            if (makeOb != null) {
                newCar.setMake(makeOb);
            } else {
                Make newMake = new Make();
                newMake.setName(make);
                newCar.setMake(newMake);
            }
        }

        Location location = (Location) createQuery("SELECT l FROM Location l WHERE l.stall = :stall")
                .setParameter("stall", locationParked)
                .getSingleResult();
        if (location != null) {
            newClaim.setLocation(location);
        } else {
            Location newLocation = new Location();
            newLocation.setStall(locationParked);
            newClaim.setLocation(newLocation);
        }

        persist(newClaim);
        getFacesMessages().add(FacesMessage.SEVERITY_INFO, "Claim created successfully");
        clear();
    }

    @End
    public void edit() {
    }
    
    @End
    public void clear() {
        customerName = null;
        locationParked = null;
        parkedBy = null;
        licensePlate = null;
        make = null;
        color = null;
        carWash = false;
        comments = null;
        damageReport = null;
    }
    
    @Destroy
    @Remove
    public void destroy() {
    }
}
